package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import nickname.HttpConnectionFactory;

import org.apache.log4j.Logger;
import org.junit.Test;

public class GGQueries {

	final static int MIN_GG_NUMBER = 400;
	final static int MAX_GG_NUMBER = 666;
	public static final int PAYMENT_REQUIRED = 402;
	public static final int OK = 200;
	final Logger logger = Logger.getLogger(GGQueries.class);

	@Test
	public void findPaiedNumbers() throws Exception {
		
		for (int ggNumber = MIN_GG_NUMBER; ggNumber <= MAX_GG_NUMBER; ggNumber++) {
			logger.debug("Sprawdzam numer: " + ggNumber);
			
			URL url = new URL("http://mobile.gadu-gadu.pl/appmsg/appsvc/mobile.asp?version=mgg1.4.1/android&fmnumber=" + ggNumber
					+ "&version=mgg1.4.1/android&lastmsg=0");
			
			HttpURLConnection connection = HttpConnectionFactory.createHttpConnection(url);
			int responseCode = connection.getResponseCode();
			
			if(responseCode != PAYMENT_REQUIRED && responseCode == OK ) {
				logger.warn("\tZnalazlem oplacony numer :" + ggNumber);
				BufferedReader reader =  new BufferedReader(new InputStreamReader(connection.getInputStream()));
				logger.debug(reader.readLine()); //w zupelnosci wystarczy
			}
			
			connection.disconnect();
		}
	}
	
	@Test
	public void checkPayments() throws Exception {
		int ggNumber = 1337;
		URL url = new URL("http://mobile.gadu-gadu.pl/appmsg/appsvc/mobile.asp?version=mgg1.4.1/android&fmnumber=" + ggNumber
				+ "&version=mgg1.4.1/android&lastmsg=0");
		
		HttpURLConnection httpURLConnection = HttpConnectionFactory.createHttpConnection(url);
		
		if (httpURLConnection.getResponseCode() == PAYMENT_REQUIRED) {
			logger.info("zadanie zaplaty za numer: " + ggNumber);
		}
		
		httpURLConnection.disconnect();
	}
	
	
}
